@echo off
setlocal enabledelayedexpansion

:: check if MSVC path is enabled
if "%VCINSTALLDIR%" == "" (
	echo Please set up Microsoft visual C compiler first!
	goto :eof
)

:: trunk
if not exist include\wx\msw\setup.h (
	echo copying setup0.h to setup.h
	copy include\wx\msw\setup0.h include\wx\msw\setup.h >nul
	if errorlevel 1 goto eof
)

:: create backup of original setup file
if not exist include\wx\msw\setup.h.bak (
	echo creating setup.h.bak
	copy include\wx\msw\setup.h include\wx\msw\setup.h.bak >nul
	if errorlevel 1 goto eof
)

:: create copy of setup.h which we will work on
echo Creating working copy of setup.h
copy include\wx\msw\setup.h.bak my_setup.h >nul
if errorlevel 1 goto eof

:: Parse the 'components' file, and fix our copy of setup.h accordingly
echo Parsing... (takes ages)
if "%2" == "test" goto :skip_parsing
for /f "delims==" %%a in (components) do (
	for /f "tokens=1,2" %%b in ('echo %%a') do (
		set NAME=%%b
		set DEFNAME=%%c
		set VAL=2
		if "!NAME:~0,1!" == "-" (
			set VAL=0
		)
		if "!NAME:~0,1!" == "+" (
			set VAL=1
		)
		rem echo Name=!DEFNAME! Val=!VAL!
		if not "!DEFNAME!" == "-" (
			if not "!VAL!" == "2" (
				call :setuseflag !DEFNAME! !VAL!
				if errorlevel 1 goto eof
			)
		)
	)
)
:skip_parsing

set CFLAGS=
set CPPFLAGS=
set OPT=UNICODE=1 USE_HTML=0 USE_MEDIA=0 USE_XRC=0 
set OPT=%OPT% USE_AUI=1 USE_RICHTEXT=0 USE_OPENGL=0 USE_ODBC=0 USE_QA=0
set OPT=%OPT% USE_EXCEPTIONS=0 USE_THREADS=0 USE_CAIRO=0

rem wxABORT_ON_CONFIG_ERROR must be set explicitly, otherwise include/msw/chkconf.h 
rem fucks up settings nstead of warning us, and then include/chkconf.h bitches 
rem about settings being wrong
rem set CPPFLAGS=/DwxABORT_ON_CONFIG_ERROR

rem wxMSW-2.8.12 wxIsPlatform64Bit() unconditionally uses wxDynamicLibrary
rem call :setuseflag DYNLIB_CLASS 1
rem if errorlevel 1 goto eof
rem call :setuseflag DYNAMIC_LOADER 1
rem if errorlevel 1 goto eof

rem wxMSW-2.8.12 wxControlContainer unconditionally uses wxRadioButton
call :setuseflag RADIOBTN 1
if errorlevel 1 goto eof

if "%1" == "release" (
	set OPT=%OPT% BUILD=release SHARED=0 DEBUG_FLAG=0 USE_RTTI=1
	set CFLAGS=/GL
	set CPPFLAGS=/GL
	set LDFLAGS=/LTCG
	call :setuseflag CRASHREPORT 0
	call :setuseflag DEBUGREPORT 0
	call :setuseflag STACKWALKER 0
	call :setuseflag ON_FATAL_EXCEPTION 0
	del lib\vc_lib\mswu\wx\setup.h
	goto :endbuild
)
if "%1" == "debug" (
	set OPT=%OPT% BUILD=debug SHARED=1 DEBUG_FLAG=1 USE_RTTI=1
	del lib\vc_dll\mswud\wx\setup.h
	goto :endbuild
)
echo Specify: debug or release
goto :done
:endbuild

echo Copying our setup.h to include\wx\msw\
copy my_setup.h include\wx\msw\setup.h
if errorlevel 1 goto eof
set OPT=%OPT% CFLAGS="%CFLAGS%" CPPFLAGS="%CPPFLAGS%" LDFLAGS="%LDFLAGS%"
echo %OPT% >opts

echo Building
cd build\msw
nmake /f makefile.vc %OPT% clean
nmake /f makefile.vc %OPT%
goto :done

:setuseflag
findstr /r /C:"#[ \t]*define wxUSE_%1" my_setup.h >nul
if errorlevel 1 (
	echo Can't find wxUSE_%1
	pause
	goto :done
)
echo wxUSE_%1=%2 
cscript /nologo sed.vbs "s/#[ \t]*define wxUSE_%1\s+[0-1]/#define wxUSE_%1 %2/" <my_setup.h >my_setup2.h
if errorlevel 1 (
	echo Error executing sed
	pause
	goto eof
)
move /Y my_setup2.h my_setup.h >nul
if errorlevel 1 (
	echo Error copying my_setup2.h to my_setup.h
	echo Error copying my_setup2.h to my_setup.h
	goto eof
)
goto :eof

:done
cd ..\..
copy include\wx\msw\setup.h.bak include\wx\msw\setup.h
:eof